var app = getApp();

Page({
    data: {
        mdoaltoggle: !0,
        sign_up: [ {
            name: "姓名"
        }, {
            name: "手机号"
        } ]
    },
    KeyName: function(e) {
        this.setData({
            name: e.detail.value
        });
    },
    KeyMobile: function(e) {
        this.setData({
            mobile: e.detail.value
        });
    },
    ckwz: function(e) {
        console.log(e.currentTarget.dataset.jwd);
        var t = e.currentTarget.dataset.jwd.split(",");
        console.log(t);
        wx.openLocation({
            latitude: Number(t[0]),
            longitude: Number(t[1]),
            name: this.data.store.store_name,
            address: this.data.store.address
        });
    },
    onLoad: function(e) {
        var t = this;
        app.setNavigationBarColor(this), app.getUrl(t), console.log(e), t.setData({
            options: e
        }), app.getUserInfo(function(e) {
            console.log(e), t.setData({
                user_id: e.id,
                openid: e.openid
            });
        }), t.refresh();
    },
    refresh: function(e) {
        var o = this;
        app.util.request({
            url: "entry/wxapp/GroupType",
            cachetime: "0",
            success: function(e) {
                console.log("分类列表", e), o.setData({
                    nav_array: e.data
                });
            }
        }), app.util.request({
            url: "entry/wxapp/GoodsInfo",
            cachetime: "0",
            data: {
                goods_id: o.data.options.id
            },
            success: function(e) {
                console.log("商品详情", e);
                var t = e.data.goods;
                1 == o.data.options.type ? (t.yh = (Number(t.y_price) - Number(t.dd_price)).toFixed(2), 
                t.money = t.dd_price) : (t.yh = (Number(t.y_price) - Number(t.pt_price)).toFixed(2), 
                t.money = t.pt_price), o.setData({
                    goods: e.data.goods
                }), o.store_info(e.data.goods.store_id);
            }
        });
    },
    store_info: function(e) {
        var n = this;
        app.util.request({
            url: "entry/wxapp/StoreInfo",
            cachetime: "0",
            data: {
                id: e
            },
            success: function(e) {
                console.log(e.data);
                var t = e.data.store[0], o = e.data.store[0].coordinates.split(","), a = {
                    lng: Number(o[1]),
                    lat: Number(o[0])
                };
                console.log(a), n.setData({
                    store: t
                });
            }
        });
    },
    alone: function(e) {
        wx.showLoading({
            title: "正在提交报名",
            mark: !0
        });
        var t = this;
        t.setData({
            place_num: 2
        });
        var o = t.data, a = o.goods, n = o.options, i = o.nav_array, s = o.user_id;
        for (var r in i) if (i[r].id == a.type_id) var d = i[r].name;
        if (1 == n.type) var c = a.dd_price; else c = a.pt_price;
        var l = c;
        1 == t.confirm_info() && app.util.request({
            url: "entry/wxapp/SaveGroupOrder",
            data: {
                user_id: s,
                goods_id: a.id,
                logo: a.logo,
                store_id: a.store_id,
                goods_name: a.name,
                goods_type: d,
                price: c,
                goods_num: 1,
                money: l,
                receive_name: o.name,
                receive_tel: o.mobile,
                receive_address: o.store.address,
                type: o.options.type,
                pay_type: 1,
                kt_num: o.options.kt_num,
                group_id: n.group_id,
                dq_time: a.end_time,
                xf_time: a.xf_time
            },
            success: function(e) {
                console.log(e), console.log("确定调用"), "商品已销售完毕或拼团已失效" == e.data ? wx.showModal({
                    title: "温馨提示",
                    content: "商品已售完",
                    success: function(e) {
                        e.cancel && wx.reLaunch({
                            url: "../inde/index"
                        });
                    }
                }) : t.pay(e.data, l);
            }
        });
    },
    confirm_info: function(e) {
        var t = this, o = t.data;
        if (console.log(o), 0 < Number(o.goods.inventory)) if (null == o.name || "" == o.name) wx.showModal({
            title: "温馨提示",
            content: "请输入您的姓名"
        }), t.setData({
            place_num: 1
        }), wx.hideLoading(); else {
            if (null != o.mobile && "" != o.mobile) return !0;
            wx.showModal({
                title: "温馨提示",
                content: "请输入您的联系电话"
            }), t.setData({
                place_num: 1
            }), wx.hideLoading();
        } else wx.showModal({
            title: "温馨提示",
            content: "商品库存不足，无法购买"
        }), t.setData({
            place_num: 1
        }), wx.hideLoading();
    },
    pay: function(e, t) {
        var o = this;
        console.log("调用微信支付");
        var a = o.data.openid;
        app.util.request({
            url: "entry/wxapp/GroupPay",
            data: {
                order_id: e,
                money: t,
                openid: a
            },
            success: function(e) {
                console.log(e), wx.requestPayment({
                    timeStamp: e.data.timeStamp,
                    nonceStr: e.data.nonceStr,
                    package: e.data.package,
                    signType: e.data.signType,
                    paySign: e.data.paySign,
                    success: function(e) {
                        console.log(e), wx.hideLoading(), wx.showToast({
                            title: "支付成功"
                        }), setTimeout(function() {
                            o.setData({
                                place_num: 2
                            }), wx.redirectTo({
                                url: "index"
                            });
                        }, 1500);
                    },
                    fail: function(e) {
                        console.log(e), wx.showLoading({
                            title: "支付失败"
                        }), setTimeout(function() {
                            wx.hideLoading(), o.setData({
                                place_num: 2
                            }), wx.reLaunch({
                                url: "../index/index"
                            });
                        }, 1500);
                    }
                });
            }
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {}
});